﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace EncryptProject
{
    /// <summary>
    /// 仅用于加解密的测试
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                /*
                Console.WriteLine("------------------------------");
                Console.WriteLine("**********  机器码  **********");
                Console.WriteLine("------------------------------");
                Console.WriteLine(new SoftReg().GetMNum());
                Console.WriteLine(Environment.NewLine);
                 */

                Console.WriteLine("------------------------------");
                Console.WriteLine("********** MD5 加密 **********");
                Console.WriteLine("------------------------------");

                string pwd;
                Console.Write(Environment.NewLine);
                Console.Write("Password（密码）：");
                pwd = Console.ReadLine();
                string md5String = EncryptAlgorithmHelper.MD5Hash(pwd);
                Console.WriteLine(md5String);
                Console.WriteLine(Environment.NewLine);



                // eg: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DataBase\db_VWMS.mdb;Persist Security Info=True;Jet OLEDB:Database Password=Fanxuanxuan123
                string connectionStringFormat = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DataBase\{0}.mdb;Persist Security Info=True;Jet OLEDB:Database Password={1}";
                string password, dbName, key;

                Console.WriteLine("------------------------------");
                Console.WriteLine("***** DES 加密（数据库） *****");
                Console.WriteLine("------------------------------");

                Console.Write(Environment.NewLine);
                Console.Write("Password（密码）：");
                password = Console.ReadLine();
                Console.Write("DB Name（数据库名）：");
                dbName = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(dbName))
                {
                    dbName = "db_VWMS";
                }
                Console.Write("Key（无密钥请直接按回车键）：");
                key = Console.ReadLine();

                string connectionString = string.Format(connectionStringFormat, dbName, password);

                Console.Write(Environment.NewLine);
                Console.WriteLine("DES加密：");
                string encryptString = EncryptAlgorithmHelper.Encrypt2(connectionString, key);
                Console.WriteLine(encryptString);

                Console.Write(Environment.NewLine);
                Console.WriteLine("DES解密：");
                string decryptString = EncryptAlgorithmHelper.Decrypt2(encryptString, key);
                Console.WriteLine(decryptString);
                Console.WriteLine(Environment.NewLine);



                Console.WriteLine("------------------------------");
                Console.WriteLine("********** DES 加密 **********");
                Console.WriteLine("------------------------------");

                string inputDESString; // 密码明文
                string extDESString; // 密码附加信息


                Console.Write(Environment.NewLine);
                Console.Write("Password（密码）：");
                inputDESString = Console.ReadLine();
                Console.Write("Ext（附加信息）：");
                extDESString = Console.ReadLine();

                Console.Write(Environment.NewLine);
                Console.WriteLine("DES加密：");
                string desString = EncryptAlgorithmHelper.Encrypt2(inputDESString, extDESString);
                Console.WriteLine(desString);
                Console.WriteLine(Environment.NewLine);



                Console.WriteLine("------------------------------");
                Console.WriteLine("++++++++++ AES 加密 ++++++++++");
                Console.WriteLine("------------------------------");

                string inputString; // 密码明文
                string extString; // 密码附加信息


                Console.Write(Environment.NewLine);
                Console.Write("Password（密码）：");
                inputString = Console.ReadLine();
                Console.Write("Ext（附加信息）：");
                extString = Console.ReadLine();

                Console.Write(Environment.NewLine);
                Console.WriteLine("AES加密：");
                string aesString = EncryptAlgorithmHelper.Encrypt(inputString, extString);
                Console.WriteLine(aesString);
                Console.WriteLine(Environment.NewLine);
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Console.ReadKey(true);
            }
        }
    }
}
